package com.xxxx.rpc.service.impl;

import com.xxxx.common.result.ShopPageInfo;
import com.xxxx.rpc.service.SearchService;
import com.xxxx.rpc.vo.GoodsVo;
import org.apache.dubbo.config.annotation.Service;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索实现类
 */
@Service(interfaceClass = SearchService.class)
@Component
public class SearchServiceImpl implements SearchService {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Override
    public ShopPageInfo<GoodsVo> doSearch(String searchStr, Integer pageNum, Integer pageSize) {
        //构建查询对象
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                //关键词查询
                .withQuery(QueryBuilders.multiMatchQuery(searchStr, "goodsName"))
                //分页
                .withPageable(PageRequest.of((pageNum - 1) * pageSize, pageSize))
                //设置高亮样式
                .withHighlightBuilder(new HighlightBuilder().field("goodsName")
                        .preTags("<span style='color:red'>")
                        .postTags("</span>"))
                .build();

        //查询信息
        SearchHits<GoodsVo> searchHits = elasticsearchRestTemplate.search(query, GoodsVo.class, IndexCoordinates.of("shop"));
        //获取命中数
        long totalHits = searchHits.getTotalHits();

        if (0 > totalHits){
            return null;
        }

        List<GoodsVo> goodsVoList = new ArrayList<>();

        for (SearchHit<GoodsVo> searchHit : searchHits) {

            String id = searchHit.getId();
            float score = searchHit.getScore();
            GoodsVo goodsVo = searchHit.getContent();
            //获取所有高亮字段
            List<String> goodsNameHlList = searchHit.getHighlightFields().get("goodsName");
            //将高亮字段设置到每一个GoodsVo
            goodsVo.setGoodsNameH1(goodsNameHlList.get(0));

            System.out.println("id = " + id);
            System.out.println("score = " + score);
            System.out.println("content = " + goodsVo);
            //System.out.println("hl = " + goodsNameHlList.get(0));
            System.out.println("======================================");

            goodsVoList.add(goodsVo);
        }

        ShopPageInfo<GoodsVo> shopPageInfo = new ShopPageInfo<GoodsVo>(pageNum,pageSize, (int) totalHits);
        shopPageInfo.setResult(goodsVoList);

        return shopPageInfo;
    }
}
